package com.shujia.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Mutation;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

// 基于HBase中的'stu'表，统计班级人数 最终结果写回HBase
public class Demo06MRReadAndWriteHBase {
    // Map任务
    public static class MyMapper extends TableMapper<Text, IntWritable> {
        @Override
        protected void map(ImmutableBytesWritable key, Result value, Mapper<ImmutableBytesWritable, Result, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            // key 对应Rowkey  value 对应一条数据
            String clazz = Bytes.toString(value.getValue("info".getBytes(), "clazz".getBytes()));
            context.write(new Text(clazz), new IntWritable(1));
        }
    }

    // Reduce任务
    public static class MyReducer extends TableReducer<Text, IntWritable, NullWritable> {
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, NullWritable, Mutation>.Context context) throws IOException, InterruptedException {
            int cnt = 0;
            for (IntWritable value : values) {
                cnt += value.get();
            }

            Put put = new Put(key.getBytes());
            put.addColumn("cf1".getBytes(),"cnt".getBytes(),(cnt+"").getBytes());

            context.write(NullWritable.get(),put);

        }
    }

    // Driver端
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

        Configuration conf = HBaseConfiguration.create();
        conf.set("fs.defaultFS", "hdfs://master:9000");
        conf.set("hbase.zookeeper.quorum", "master:2181,node1:2181,node2:2181");

        Job job = Job.getInstance(conf);

        job.setJobName("Demo06MRReadAndWriteHBase");
        job.setJarByClass(Demo06MRReadAndWriteHBase.class);

        Scan scan = new Scan();

        // 配置Map任务
        // 由于处理的是HBase中的数据 故HBase提供了一个工具 可以对Map任务进行配置
        TableMapReduceUtil.initTableMapperJob(
                "stu"
                ,scan
                ,MyMapper.class
                ,Text.class
                ,IntWritable.class
                ,job
        );


        // 配置Reduce任务
        TableMapReduceUtil.initTableReducerJob(
                "clazz_cnt"
                , MyReducer.class
                ,job
        );

        job.waitForCompletion(true);

    }
    /**
     * create 'clazz_cnt','cf1'
     *
     * hadoop jar hbase-1.0-SNAPSHOT-jar-with-dependencies.jar com.shujia.hbase.Demo06MRReadAndWriteHBase
     */


}
